import 'package:flutter/widgets.dart';
import 'package:full_display/page/login/login_api.dart';
import 'package:full_display/page/login/models/login_model.dart';

class LoginProvider extends ChangeNotifier {
  static const logheader = 'LoginProvider=>';

  static const titleList = ['手机号登录', '邮箱登录'];

  static const areaCode = ['+1', '+7', '+20', '+27', '+30', '+31', '+32', '+33', '+34', '+36', '+37', '+38', '+39', '+40', '+41', '+43',
    '+44', '+45', '+46', '+47', '+48', '+49', '+51', '+52', '+53', '+54', '+55', '+56', '+57', '+58', '+60',
    '+61', '+62', '+63', '+64', '+65', '+66', '+81', '+82', '+84', '+86', '+90', '+91', '+92', '+93', '+94',
    '+95', '+98', '+212', '+213', '+216', '+218', '+220', '+221', '+223', '+224', '+225', '+226', '+227',
    '+228', '+229', '+230', '+231', '+232', '+233', '+234', '+235', '+236', '+237', '+239', '+241', '+242',
    '+243', '+244', '+247', '+248', '+249', '+251', '+252', '+253', '+254', '+255', '+256', '+257', '+258',
    '+260', '+261', '+262', '+263', '+264', '+265', '+266', '+267', '+268', '+327', '+331', '+350', '+351',
    '+352', '+353', '+354', '+355', '+356', '+357', '+358', '+359', '+370', '+371', '+372', '+373', '+374',
    '+375', '+376', '+377', '+378', '+380', '+381', '+385', '+386', '+420', '+421', '+423', '+501', '+502', '+503',
    '+504', '+505', '+506', '+507', '+509', '+591', '+592', '+593', '+594', '+595', '+596', '+597', '+598',
    '+599', '+673', '+674', '+675', '+676', '+677', '+679', '+682', '+684', '+685', '+689', '+850', '+852',
    '+853', '+855', '+856', '+880', '+886', '+960', '+961', '+962', '+963', '+964', '+965', '+966', '+967',
    '+968', '+971', '+972', '+973', '+974', '+976', '+977', '+992', '+993', '+994', '+995', '+1242', '+1246',
    '+1264', '+1268', '+1345', '+1441', '+1664', '+1670', '+1671', '+1758', '+1784', '+1787', '+1809',
    '+1876', '+1890'
  ];

  var loginApi = LoginApi();

  ///{username: 13128901052, password: 123456, grant_type: password}
  final _phoneMap={'username':'','password':'','grant_type':'password'};
  var _emailMap={'username':'','password':'','grant_type':'password'};

  ///type:0/1
  ///0手机号登录；1邮箱登录
  loginByPhoneOrEmail(int type,Function(dynamic value) callback){
    if(type==0){
      _goLogin(_phoneMap['username']!, _phoneMap['password']!, callback);
    }else{
      _goLogin(_emailMap['username']!, _emailMap['password']!, callback);
    }
  }

  LoginModel? loginModel;

  _goLogin(String username, String password, Function(dynamic value) callback) {
    if (username.isNotEmpty /*&& !username.contains("@")*/) {
      loginApi.goLogin(username, password, success: (result) {
        loginModel = LoginModel.fromJson(result);
        callback.call(result);
      }, failure: (error) {
        callback.call(error);
      });
    }
  }

  ///更新map
  updatePhoneMap(map) {
    _phoneMap['username'] = map['phone'];
    _phoneMap['password'] = map['password'];
  }
  ///更新map
  updateEmailMap(map) {
    _emailMap['username'] = map['username'];
    _emailMap['password'] = map['password'];
  }
}
